from django.contrib import admin
from django.utils.html import format_html
from django.db import models
from mdeditor.widgets import MDEditorWidget  # 富文本框支持
from .models import Lesson_note, Lesson_type, Payment, Scheduling, Consume_note
from home.models import MyUser


# Register your models here.


class Lesson_type_admin(admin.ModelAdmin):
    """课程类型"""

    list_display = [
        "id",
        "lesson_type",
        "price",
        "duration",
        "teacher",
    ]
    # 设置哪些字段可以点击进入编辑界面
    list_display_links = (
        "id",
        "lesson_type",
    )
    list_filter = ("teacher",)
    # 使用搜索来代替选择
    autocomplete_fields = ("teacher",)


class Payment_admin(admin.ModelAdmin):
    """交费"""

    list_display = [
        "id",
        "user",
        "note_user",
        "lesson_type",
        "time",
        "teacher",
        "workload",
        "workload_note",
    ]
    formfield_overrides = {models.TextField: {"widget": MDEditorWidget}}
    # 设置哪些字段可以点击进入编辑界面
    list_display_links = (
        "id",
        "user",
        "note_user",
    )
    list_filter = ("user", "lesson_type", "time")
    #search_fields = ("user__real_name",)
    # 详情页分组
    fieldsets = (
        (
            "基本",
            {
                "fields": [
                    "user",
                    "lesson_type",
                    "time",
                    "teacher",
                    "workload",
                ]
            },
        ),
        (
            "高级",
            {
                "fields": [
                    "note",
                    "money",
                ]
            },
        ),
    )
    # 使用搜索来代替选择
    autocomplete_fields = ("user", "teacher")
    # 时间倒序
    ordering = ("-time",)

    def save_model(self, request, obj, form, change):
        # 重写保存功能
        user = request.user
        user = MyUser.objects.filter(user=user)
        obj.note_user = user[0]
        if not change:  # 不是更新就是新增
            user = obj.user
            user.money += obj.money  # 加钱
            user.workload += obj.workload  # 加课时
            obj.workload_note = user.workload
            user.save()
        super().save_model(request, obj, form, change)


class Scheduling_admin(admin.ModelAdmin):
    """排课"""

    list_display = [
        "id",
        "title",
        "学员",
        "lesson_type",
        "start",
        "status",
        "teacher",
        "note_user",
    ]

    # 设置哪些字段可以点击进入编辑界面
    list_display_links = (
        "id",
        "title",
    )
    list_filter = (
        "user",
        "lesson_type",
        "start",
        "teacher",
    )
    #search_fields = ("user__real_name",)
    formfield_overrides = {models.TextField: {"widget": MDEditorWidget}}

    # 使用搜索来代替选择
    autocomplete_fields = ("user", "teacher",)
    # 时间倒序
    ordering = ("-start", "teacher",)
    # 详情页分组
    fieldsets = (
        (
            "基本",
            {
                "fields": [
                    "user",
                    "title",
                    "lesson_type",
                    "start",
                    "teacher",
                ]
            },
        ),
        (
            "高级",
            {
                "fields": [
                    "notice",
                    "status",
                ]
            },
        ),
    )

    def save_model(self, request, obj, form, change):
        # 重写保存功能
        user = request.user
        user = MyUser.objects.filter(user=user)
        obj.note_user = user[0]
        super().save_model(request, obj, form, change)

    def 学员(self, obj):
        """返回学员列表"""
        names = "，".join([str(i) for i in obj.user.all()])
        return names


class Lesson_note_admin(admin.ModelAdmin):
    """上课记录"""

    list_display = [
        "id",
        "title",
        "学员",
        "lesson_type",
        "teacher",
        "start",
        "end",
        "consume",
        "查看记录", # 查看按钮
    ]

    # 设置哪些字段可以点击进入编辑界面
    list_display_links = (
        "id",
        "title",
    )
    list_filter = (
        "title",
        "user",
        "teacher",
        "lesson_type",
        "start",        
    )
    # search_fields = ["=title"]# 搜索框
    formfield_overrides = {models.TextField: {"widget": MDEditorWidget}}
    # search_fields = ("user__real_name",)
    
    # 直接在列表页可编辑项
    #list_editable=("consume",)

    # 使用搜索来代替选择
    autocomplete_fields = ("user","teacher",)
    raw_id_fields = (
        "teacher",
        "notice",
    )
    # 时间倒序
    ordering = ("-start",)
    # 详情页分组
    fieldsets = (
        (
            "基本",
            {
                "fields": [
                    "user",
                    "title",
                    "lesson_type",
                    "start",
                    "end",
                    "teacher",
                    "consume",
                    "notice",
                ]
            },
        ),
        (
            "高级",
            {
                "fields": [
                    "note",
                    "status",
                ]
            },
        ),
    )

    # 按时间分层
    # date_hierarchy = "start" # 特别消耗资源，还是算了

    def save_model(self, request, obj, form, change):
        # 重写保存功能
        user = request.user
        user = MyUser.objects.filter(user=user)
        obj.note_user = user[0]
        super().save_model(request, obj, form, change)

    def 学员(self, obj):
        """返回学员列表"""
        names = "，".join([str(i) for i in obj.user.all()])
        return names

    @admin.display(description="备注")
    def 查看记录(self, obj):
        """查看上课记录"""
        return format_html(f'<a href="/service/lesson_note/{obj.id}" target="_blank">查看记录</a>')

class Consume_note_admin(admin.ModelAdmin):
    """消课记录"""

    list_display = [
        "id",
        "user",
        "lesson_type",
        "teacher",
        "note_user",
        "consume",
        "上课记录",
        "workload_note",
    ]
    # 设置哪些字段可以点击进入编辑界面
    list_display_links = ("id", "user")
    list_filter = (
        "user",
        "lesson_type",
        "teacher",
    )
    #search_fields = ("user__real_name",)
    formfield_overrides = {models.TextField: {"widget": MDEditorWidget}}

    # 使用搜索来代替选择
    autocomplete_fields = (
        "user",
    )
    raw_id_fields = (
        "teacher",
        "lesson_note",
    )
    # 时间倒序
    # ordering = ("-start",)

    # 详情页分组
    fieldsets = (
        (
            "基本",
            {
                "fields": [
                    "user",
                    "lesson_type",
                    "teacher",
                    "consume",
                    "lesson_note",
                ]
            },
        ),
        (
            "高级",
            {
                "fields": [
                    "note",
                    "status",
                ]
            },
        ),
    )

    def save_model(self, request, obj, form, change):
        # 重写保存功能
        user = request.user
        user = MyUser.objects.filter(user=user)
        obj.note_user = user[0]
        if not change:  # 不是更新就是新增
            user = obj.user
            user.money -= obj.lesson_type.price * obj.consume  # 扣费
            user.workload -= obj.consume  # 扣课时
            user.save()
            obj.workload_note = user.workload
        super().save_model(request, obj, form, change)

    def 上课记录(self, obj):
        """重写消课记录返回值"""
        names = "，".join([str(i) for i in obj.lesson_note.all()])
        return names


admin.site.register(Lesson_type, Lesson_type_admin)
admin.site.register(Payment, Payment_admin)
admin.site.register(Scheduling, Scheduling_admin)
admin.site.register(Lesson_note, Lesson_note_admin)
admin.site.register(Consume_note, Consume_note_admin)
